# -*- coding: utf-8 -*-

# Copyright 2009, NexR (http://nexr.co.kr)
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.


class CloudException(Exception):
    pass

class CloudQueryException(CloudException):
    def __init__(self, detail, errors=None):
        message = u'%d %s' % (detail['status'], detail['reason'])
        super(CloudQueryException, self).__init__(message)
        self.detail = detail
        # TODO: errors attribute will be deprectated.
        self.errors = detail['errors']

    def __unicode__(self):
        if self.detail.has_key('errors') and len(self.details['errors']) > 0:
            flatted_errors = map(lambda e: '%s: %s' % (e[0], e[1]), self.detail['errors'])
            errors = u'(%s)' % (u', '.join(flatted_errors))
        else:
        	errors = u''
        if self.detail.has_key('requestId'):
            content = u'CloudQueryException[requestId=%s]: %s %s' % (self.detail['requestId'], self.message, errors)
        else:
            content = u'CloudQueryException: %s %s' % (self.message, errors)
        return content.strip()

